﻿@{
    ViewBag.Title = "InvoiceClearList";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section scripts{
    <script type="text/javascript">

        //2017-07-24 zch 增加按键保存
        document.onkeydown = function (event) {
            var e = event || window.event || arguments.callee.caller.arguments[0];
            if (e && e.keyCode == 27) { // 按 Esc
                //要做的事情
            }
            if (e && e.keyCode == 38) { // 按 向上
                //要做的事情
                if (!$("#FillDetailDialog").parent().is(":hidden")) {
                    insertOneRow();
                }
                if (!$("#FillDialogCon").parent().is(":hidden")) {
                    insertOneRowCon();
                }
            }
            if (e && e.keyCode == 13) { // enter 键
                //如果 tt 表格是打开的就处理tt
                if (!$("#FillDetailDialog").parent().is(":hidden")) {
                    keySaverow();
                }
                if (!$("#FillDialogCon").parent().is(":hidden")) {
                    keySaverowCon();
                }
            }
        };


        $(document).ready(function () {
            initListCombo();//加载列表的页面下拉框内容
            bindClearGrid();
            $.setGridWithSearchBar("#TableForm", "#SearchBar", "#gridClearList");
        });

        function initListCombo() {
            $('#zAccidentTimeStart').datebox('setValue', $.GetMonthFirstday());
            $('#zAccidentTimeEnd').datebox('setValue', $.GetTomorrow());

        } //页面初始化结束

        function reSearch() {
            $('#gridClearList').datagrid({
                pageNumber: 1,
                queryParams: {
                    beginTime: $('#zAccidentTimeStart').datebox('getValue'),
                    endTime: $('#zAccidentTimeEnd').datebox('getValue'),
                    clearNumber: $('#zInvoiceNumber').textbox('getValue')
                }
            });
        }

        function bindClearGrid() {
            $('#gridClearList').datagrid({
                width: 'auto',
                height: 'auto',
                fitColumns: true,
                singleSelect: true,
                loadMsg: '数据加载中请稍后……',
                url: '@Url.Content("~/IM/I_InvoiceApproval/InvoiceClearLoad")',
                striped: true,
                pagination: true,//启用分页，默认每页10行
                rownumbers: true,//显示页码，默认 提供 10 - 50 的页容量选择下拉框
                pageSize: 15,//设置 页容量5为
                pageList: [10, 15, 20],//设置 页容量下拉框
                sortName: 'ClearDate',
                sortOrder: 'desc',
                remoteSort: true,
                queryParams: {
                    beginTime: $('#zAccidentTimeStart').datebox('getValue'),
                    endTime: $('#zAccidentTimeEnd').datebox('getValue'),
                    clearNumber: $('#zInvoiceNumber').textbox('getValue')
                },
                toolbar: [
                   {
                       id: 'btnAdd',
                       text: '单个销号',
                       iconCls: 'icon-custom-cGoods',
                       iconWidth: 22,
                       handler: function () {
                           $('#FillDetailDialog').dialog('open');

                           initDetailGrid();
                       }
                   },
                    {
                        id: 'btnAdd',
                        text: '连号销号',
                        iconCls: 'icon-custom-cGoods',
                        iconWidth: 22,
                        handler: function () {
                            $('#FillDialogCon').dialog('open');
                            initDetailGridCon();
                        }
                    }, {
                        id: 'btnHelp',
                        text: '帮助',
                        iconCls: 'icon-help',
                        handler: function () {
                            $('#DialogHelp').dialog('open');
                           
                        }
                    }
                ],
                frozenColumns: [[
                     { field: "ID", title: "ID", hidden: true, sortable: false },
                       {
                           field: "deletez", title: "撤销", width: '6%', align: "center",
                           formatter: function (value, row, index) {
                               return '<a  id="adelete" class="linkURLdelete" onclick="deleteDetailURL(\'' + row.ID + '\');"  href="javascript:void(0)">撤销</a>';
                           }
                       },
                   { field: "ClearDate", title: "销号日期", width: '12%', align: "center", sortable: true, formatter: $.formatDate },
                   { field: "ClearPerson", title: "销号人", width: '6%', align: "center", sortable: true },
                   { field: "ClearNumber", title: "消除发票号", width: '15%', align: "center", sortable: true }
                ]]
            });
        } //列表加载结束

        //2016-5-26 新增删除
        function deleteDetailURL(deleterowID) {

            $.messager.confirm('提示', '您确定要撤销此内容吗？', function (r) {
                if (r) {
                    document.getElementById('adelete').disabled = true;
                    $.ajax({
                        type: 'POST',
                        url: '@Url.Content("~/IM/I_InvoiceApproval/deleteINClear")',
                        data:
                            {
                                deleteRowID: deleterowID
                            },
                        dataType: "json",
                        success: function (jsData) {
                            $.handleJsonResult(jsData, function () {
                                $("#gridClearList").datagrid("reload");
                                document.getElementById('adelete').disabled = false;

                            });
                        }
                    });
                }
            });
        }
        $(window).resize(function () {
            $.setGridWithSearchBar("#TableForm", "#SearchBar", "#gridClearList");
        });
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        function initDetailGrid() {
            $('#tt').datagrid({
                title: '手动销号',
                iconCls: 'icon-edit',
                singleSelect: true,
                columns: [[
                    {
                        field: 'ClearNumber', title: '发票号', width: 200,
                        editor: {
                            type: 'numberbox',
                            options: {
                                min: 1000000000,
                                max: 9999999999,
                                required: true
                            }
                        }
                    },
                    {
                        field: 'Action', title: '操作', width: 120, align: 'center',
                        formatter: function (value, row, index) {
                            if (row.editing) {
                                var s = '<a href="javascript:void(0)" onclick="saverow(this)">保存</a> ';
                                var c = '<a href="javascript:void(0)" onclick="cancelrow(this)">取消</a>';
                                return s + c;
                            } else {
                                var e = '<a href="javascript:void(0)" onclick="editrow(this)">编辑</a> ';
                                var d = '<a href="javascript:void(0)" onclick="deleterow(this)">删除</a>';
                                return e + d;
                            }
                        }
                    }
                ]],
                onEndEdit: function (index, row) {
                    $.ajax({
                        type: 'POST',
                        url: '@Url.Content("~/IM/I_InvoiceApproval/checkINRepeat")',
                        data:
                            {
                                ClearNumber: row.ClearNumber
                            },
                        dataType: "json",
                        success: function (jsData) {
                            $.handleJsonResult(jsData, function () {
                                //  $("#gridClearList").datagrid("reload");
                            });
                        }
                    });
                },
                onBeforeEdit: function (index, row) {
                    row.editing = true;
                    $(this).datagrid('refreshRow', index);
                },
                onAfterEdit: function (index, row) {
                    row.editing = false;
                    $(this).datagrid('refreshRow', index);
                },
                onCancelEdit: function (index, row) {
                    row.editing = false;
                    $(this).datagrid('refreshRow', index);
                }
            });
        }
        function getRowIndex(target) {
            var tr = $(target).closest('tr.datagrid-row');
            return parseInt(tr.attr('datagrid-row-index'));
        }
        function editrow(target) {
            $('#tt').datagrid('beginEdit', getRowIndex(target));
        }
        function deleterow(target) {
            $.messager.confirm('消息', '你确定要删除吗?', function (r) {
                if (r) {
                    $('#tt').datagrid('deleteRow', getRowIndex(target));
                }
            });
        }
        function saverow(target) {
            $('#tt').datagrid('endEdit', getRowIndex(target));
        }
        function cancelrow(target) {
            $('#tt').datagrid('cancelEdit', getRowIndex(target));
        }
        function insertOneRow() {
            var row = $('#tt').datagrid('getSelected');
            if (row) {
                var index = $('#tt').datagrid('getRowIndex', row);
            } else {
                index = 0;
            }
            $('#tt').datagrid('insertRow', {
                index: index,
                row: {
                    //status: 'P'
                }
            });
           $('#tt').datagrid('selectRow', index);
           $('#tt').datagrid('beginEdit', index);
           var ed = $('#tt').datagrid('getEditor', { index: index, field: 'ClearNumber' });
           if (ed) {
              // alert("focus");
               ($(ed.target).data('textbox') ? $(ed.target).textbox('textbox') : $(ed.target)).focus();
           }

        }
        //提交
        function submitGridData() {
            var rows = $('#tt').datagrid("getRows");
            $.ajax({
                type: 'POST',
                url: '@Url.Content("~/IM/I_InvoiceApproval/submitINClear")',
                data:
                    {
                        DataGridInfo: JSON.stringify(rows)
                    },
                dataType: "json",
                success: function (jsData) {
                    $.handleJsonResult(jsData, function () {
                        $("#FillDetailDialog").dialog("close");
                        $("#gridClearList").datagrid("reload");
                        //$('#tt').datagrid('loadData', { total: 0, rows: [] });
                    });
                }
            });
        }
        //2017-07-24 zch 增加按键保存
        function keySaverow() {
            var index =  $('#tt').datagrid('getRowIndex', $("#tt").datagrid('getSelected'));
            $('#tt').datagrid('endEdit', index);
        }
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        function initDetailGridCon() {
            $('#ttCon').datagrid({
                title: '连号销号',
                iconCls: 'icon-edit',
                singleSelect: true,
                columns: [[
                    {
                        field: 'ClearPerson', title: '首位发票号', width: 170,
                        editor: {
                            type: 'numberbox',
                            options: {
                                min: 1000000000,
                                max: 9999999999,
                                required: true
                            }
                        }
                    },
                    {
                        field: 'LinkTaskCode', title: '末位发票号', width: 170,
                        editor: {
                            type: 'numberbox',
                            options: {
                                min: 1000000000,
                                max: 9999999999,
                                required: true,
                                onChange: function (newValue, oldValue) {
                                    var row = $('#ttCon').datagrid('getSelected');
                                    if (row) {
                                        var index = $('#ttCon').datagrid('getRowIndex', row);
                                    } else {
                                        index = 0;
                                    }
                                    var ed1 = $('#ttCon').datagrid('getEditor', {
                                        index: index,
                                        field: 'ClearPerson'
                                    });
                                    var ed2 = $('#ttCon').datagrid('getEditor', {
                                        index: index,
                                        field: 'LinkPatientOrder'
                                    });
                                    var StartText = $(ed1.target).numberbox('getText');
                                    var CountText = (newValue) - Number(StartText) + 1;
                                    if (CountText > 0) {
                                        $(ed2.target).numberbox('setText', CountText);
                                    }
                                    else {
                                        $.messager.alert('警告', '输入有误，末位发票号比首位发票号小!');
                                    }
                                }
                            }
                        }
                    },
                    {
                        field: 'LinkPatientOrder', title: '数量', width: 50, align: 'center',
                        editor: {
                            type: 'numberbox',
                            options: {
                                min: 0,
                                max: 10000,
                                editable: false,
                                required: true,
                            }
                        }
                    },
                    {
                        field: 'ActionCon', title: '操作', width: 100, align: 'center',
                        formatter: function (value, row, index) {
                            if (row.editing) {
                                var s = '<a href="javascript:void(0)" onclick="saverowCon(this)">保存</a> ';
                                var c = '<a href="javascript:void(0)" onclick="cancelrowCon(this)">取消</a>';
                                return s + c;
                            } else {
                                var e = '<a href="javascript:void(0)" onclick="editrowCon(this)">编辑</a> ';
                                var d = '<a href="javascript:void(0)" onclick="deleterowCon(this)">删除</a>';
                                return e + d;
                            }
                        }
                    }
                ]],
                onEndEdit: function (index, row) {
                },
                onBeforeEdit: function (index, row) {
                    row.editing = true;
                    $(this).datagrid('refreshRow', index);
                },
                onAfterEdit: function (index, row) {
                    row.editing = false;
                    $(this).datagrid('refreshRow', index);
                },
                onCancelEdit: function (index, row) {
                    row.editing = false;
                    $(this).datagrid('refreshRow', index);
                }
            });
        }
        function getRowIndexCon(target) {
            var tr = $(target).closest('tr.datagrid-row');
            return parseInt(tr.attr('datagrid-row-index'));
        }
        function editrowCon(target) {
            $('#ttCon').datagrid('beginEdit', getRowIndexCon(target));
        }
        function deleterowCon(target) {
            $.messager.confirm('消息', '你确定要删除吗?', function (r) {
                if (r) {
                    $('#ttCon').datagrid('deleteRow', getRowIndexCon(target));
                }
            });
        }
        function saverowCon(target) {
            $('#ttCon').datagrid('endEdit', getRowIndexCon(target));
        }
        function cancelrowCon(target) {
            $('#ttCon').datagrid('cancelEdit', getRowIndexCon(target));
        }
        function insertOneRowCon() {
            var row = $('#ttCon').datagrid('getSelected');
            if (row) {
                var index = $('#ttCon').datagrid('getRowIndex', row);
            } else {
                index = 0;
            }
            $('#ttCon').datagrid('insertRow', {
                index: index,
                row: {
                }
            });

            $('#ttCon').datagrid('selectRow', index);
            $('#ttCon').datagrid('beginEdit', index);

            var ed = $('#ttCon').datagrid('getEditor', { index: index, field: 'ClearPerson' });
            if (ed) {
                // alert("focus");
                ($(ed.target).data('textbox') ? $(ed.target).textbox('textbox') : $(ed.target)).focus();
            }
        }

        //2017-07-24 zch 增加按键保存
        function keySaverowCon() {
            var index =  $('#ttCon').datagrid('getRowIndex', $("#ttCon").datagrid('getSelected'));
            $('#ttCon').datagrid('endEdit', index);
        }

        function submitGridDataCon() {
            var rows = $('#ttCon').datagrid("getRows");
            $.ajax({
                type: 'POST',
                url: '@Url.Content("~/IM/I_InvoiceApproval/submitINClearCon")',
                data:
                    {
                        DataGridInfo: JSON.stringify(rows)
                    },
                dataType: "json",
                success: function (jsData) {
                    $.handleJsonResult(jsData, function () {
                        $("#FillDialogCon").dialog("close");
                        $("#gridClearList").datagrid("reload");
                    });
                }
            });
        }

    </script>
}

<div id="TableForm" class="easyui-layout" style="width:auto; height:auto; margin-left: auto; margin-right: auto">
    <div data-options="region:'center',title:' 手动销号',iconCls:'icon-custom-cWuZiWeiHu'">
        <div id="SearchBar" data-options="region:'north'" style="overflow: hidden; padding: 10px">
            <table>
                <tr>
                    <td>日期：</td>
                    <td align="left" valign="middle">
                        <input class="easyui-datebox" style="width: 150px" id="zAccidentTimeStart">
                    </td>
                    <td>至：</td>
                    <td align="left" valign="middle">
                        <input class="easyui-datebox" style="width: 150px" id="zAccidentTimeEnd">
                    </td>
                    <td align="right">发票号：</td>
                    <td align="left" valign="middle">
                        <input class="easyui-textbox" style="width: 150px" id="zInvoiceNumber">
                    </td>
                    <td>
                        <a href="javascript:void(0)" id="btnsearch" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="reSearch()">查询</a>
                    </td>
                </tr>
            </table>
        </div>
        <div data-options="region:'center'" style="background: #fafafa; overflow: hidden">
            <table id="gridClearList"></table>
        </div>

    </div>
</div>

<div id="FillDetailDialog" class="easyui-dialog" title="手动销号"
     data-options="modal:true,
    collapsible:false,
    minimizable:false,
    maximizable:false,
    closed:true,
    resizable:false,
    openAnimation:'slide',
    closeAnimation:'slide',
    iconCls:'icon-custom-cWuZiWeiHu',
    onClose:function(){
      $('#tt').datagrid('loadData', { total: 0, rows: [] });
     }"
     style="width: 400px; height: auto; padding: 10px;">
    <div style="margin:10px 0">
        <table border="0">
            <tr>
                <td width="50%" align="left"><a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add'" onclick="insertOneRow()">新  增</a> </td>
                <td width="50%" align="right"><a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="submitGridData()">提  交</a></td>
            </tr>

        </table>
    </div>
    <div>
        <table id="tt"></table>
    </div>
</div>

<div id="FillDialogCon" class="easyui-dialog" title="连号销号"
     data-options="modal:true,
    collapsible:false,
    minimizable:false,
    maximizable:false,
    closed:true,
    resizable:false,
    openAnimation:'slide',
    closeAnimation:'slide',
    iconCls:'icon-custom-cWuZiWeiHu',
    onClose:function(){
      $('#ttCon').datagrid('loadData', { total: 0, rows: [] });
     }"
     style="width: 600px; height: auto; padding: 10px;">
    <div style="margin:10px 0">
        <table border="0">
            <tr>
                <td width="50%" align="left"><a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add'" onclick="insertOneRowCon()">新  增</a> </td>
                <td width="50%" align="right"><a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="submitGridDataCon()">提  交</a></td>
            </tr>

        </table>
    </div>
    <div>
        <table id="ttCon"></table>
    </div>
</div>

<div id="DialogHelp" class="easyui-dialog" title="帮助"
     data-options="modal:true,
    collapsible:false,
    minimizable:false,
    maximizable:false,
    closed:true"
     style="width: 600px; height: 300px; padding: 10px;">
 <span>在点击单个销号或者连号销号后，可支持键盘上操作：按键新增与按键保存,↑ “上箭头”表示新增， enter “回车键”表示保存 </span>
</div>


